package com.atwork.wuhua.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.MyWarAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.EvaluateSelectBean;
import com.atwork.wuhua.bean.MyWarBean;
import com.atwork.wuhua.dialog.BaseDialog;
import com.atwork.wuhua.dialog.CenterDialog;
import com.atwork.wuhua.dialog.PopEvaluate;
import com.atwork.wuhua.mvp.presenter.MyWarPresenter;
import com.atwork.wuhua.mvp.view.IMyWarActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyWarActivity extends BaseActivity implements IMyWarActivity {
    public static final String MYSUB = "1"; //我的预约
    public static final String MYWAR = "2";//我的约战
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.rv_war)
    RecyclerView rvWar;
    @BindView(R.id.img_type)
    ImageView imgType;
    @BindView(R.id.img_time_top)
    ImageView imgTimeTop;
    @BindView(R.id.img_time_boom)
    ImageView imgTimeBoom;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    @BindView(R.id.lly_type)
    LinearLayout llyType;
    @BindView(R.id.lly_no_date)
    LinearLayout llyNoDate;
    @BindView(R.id.lly_screen)
    LinearLayout llyScreen;

    private MyWarPresenter presenter;

    private MyWarAdapter myWarAdapter;
    private List<MyWarBean.DataBean> myWarBeans;
    private List<EvaluateSelectBean> evaluateSelectBeans;
    private String mType; //区分 我的预约 和 我的约战 的界面
    private int timeCode = 1; // 时间排序 1 离最近的时间 2 最远的时间

    private int action = MOVE_UPDATA;
    private int total;
    private String state = "0";//预约状态（0为全部,1为未评价,2为已评价,3为已取消)
    private String sort = "ASC";//排序方式(ASC/DESC)

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_war;
    }

    @Override
    public void initView() {
        presenter = new MyWarPresenter();
        presenter.attachView(this);
        if (getIntent().getExtras() != null) {
            mType = getIntent().getExtras().getString(ConstantsMyself.INTENTTYPE);
            if (mType.equals(MYSUB)) {
                tvTitle.setText("我的预约");
                tvType.setText("所有预约");
                llyScreen.setVisibility(View.VISIBLE);
            } else if (mType.equals(MYWAR)) {
                tvTitle.setText("我的约战");
                tvType.setText("所有约战");
                llyScreen.setVisibility(View.GONE);
            }
            initTypeSelectData();
        }

        myWarBeans = new ArrayList<>();


        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvWar.setLayoutManager(manager);

        myWarAdapter = new MyWarAdapter(mType, R.layout.item_war, myWarBeans);
        rvWar.setAdapter(myWarAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        action = MOVE_UPDATA;
        presenter.getData(mType, 1, state, sort);
    }

    @Override
    public void initData() {
        myWarAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                Intent intent = null;
                switch (view.getId()) {
                    case R.id.tv_evaluate:
                        if (mType.equals(MYSUB)) {
                            if (myWarBeans.get(position).getState_id().equals("1")) {
                                if (myWarBeans.get(position).isCan_cancel()) {
                                    CenterDialog centerDialog = new CenterDialog(MyWarActivity.this, "确定取消预约?", new CenterDialog.OnDialogClick() {
                                        @Override
                                        public void dialogClick(int switchButton) {
                                            presenter.putCancel(myWarBeans.get(position).getId());
                                        }
                                    });
                                    centerDialog.show();
                                } else {
                                    if (myWarBeans.get(position).isCan_evaluate()) {
                                        intent = new Intent(MyWarActivity.this, EvaluateActivity.class);
                                        intent.putExtra(ConstantsMyself.INTENTID, myWarBeans.get(position).getId());
                                        intent.putExtra(ConstantsMyself.INTENTFLAG, false);
                                        intent.putExtra(ConstantsMyself.INTENTNAME, myWarBeans.get(position).getSite_name());
                                        startActivity(intent);
                                    } else {

                                    }
                                }
                            } else if (myWarBeans.get(position).getState_id().equals("2")) {
                                intent = new Intent(MyWarActivity.this, EvaluateActivity.class);
                                intent.putExtra(ConstantsMyself.INTENTID, myWarBeans.get(position).getId());
                                intent.putExtra(ConstantsMyself.INTENTFLAG, true);
                                intent.putExtra(ConstantsMyself.INTENTNAME, myWarBeans.get(position).getSite_name());
                                startActivity(intent);
                            }
                        } else if (mType.equals(MYWAR)) {
                            if (myWarBeans.get(position).getState_id().equals("1")) {
                                if (myWarBeans.get(position).isCan_cancel()) {
                                    CenterDialog centerDialog = new CenterDialog(MyWarActivity.this, "确定取消约战?", new CenterDialog.OnDialogClick() {
                                        @Override
                                        public void dialogClick(int switchButton) {
                                            presenter.putCancel(myWarBeans.get(position).getId());
                                        }
                                    });
                                    centerDialog.show();
                                } else {
                                    intent = new Intent(MyWarActivity.this, SubDetailActivity.class);
                                    intent.putExtra(ConstantsMyself.INTENTID, myWarBeans.get(position).getId());
                                    intent.putExtra(ConstantsMyself.INTENTTYPE, SubDetailActivity.DETIAL);
                                    startActivity(intent);
                                }
                            }
                        }


                        break;
                    case R.id.lly_left:
                        if (mType.equals(MYWAR)) {
                            intent = new Intent(MyWarActivity.this, SubDetailActivity.class);
                            intent.putExtra(ConstantsMyself.INTENTID, myWarBeans.get(position).getId());
                            intent.putExtra(ConstantsMyself.INTENTTYPE, SubDetailActivity.DETIAL);
                            startActivity(intent);
                        }
                        break;

                }
            }
        });

        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                action = MOVE_UPDATA;
                presenter.getData(mType, 1, state, sort);
            }
        });

        myWarAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (myWarBeans.size() < total) {
                    action = MOVE_LOADING;
                    presenter.getData(mType, 1, state, sort);
                } else {
                    myWarAdapter.loadMoreEnd();//数据加载完毕
                }

            }
        }, rvWar);
    }


    @Override
    public void setData(MyWarBean myWarBean) {
        total = myWarBean.getMeta().getTotal();
        if (action == MOVE_UPDATA) {
            myWarBeans.clear();
        }
        myWarBeans.addAll(myWarBean.getData());
        myWarAdapter.setNewData(myWarBeans);
        swipe.setRefreshing(false);
        LogUtils.e("size==>" + myWarBeans.size());
    }

    @Override
    public void noData() {
        llyNoDate.setVisibility(View.VISIBLE);
    }

    @Override
    public void cancelResult() {
        presenter.getData(mType, 1, state, sort);
    }

    @OnClick({R.id.lly_type, R.id.lly_time, R.id.img_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.lly_type:
                imgType.setBackgroundResource(R.mipmap.icon_triangle_top_select);
                PopEvaluate popEvaluate = new PopEvaluate(this, evaluateSelectBeans, new PopEvaluate.OnSelectLinstener() {
                    @Override
                    public void onselect(String name, String id) {
                        LogUtils.e("name==>" + name);
                        state = id;
                        tvType.setText(name);
                        action = MOVE_UPDATA;
                        presenter.getData(mType, 1, state, sort);
                    }

                    @Override
                    public void onDismiss() {
                        imgType.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                    }
                });
                popEvaluate.showAsDropDown(view);
                break;
            case R.id.lly_time:
                if (timeCode == 1) {
                    timeCode = 2;
                    imgTimeTop.setBackgroundResource(R.mipmap.icon_triangle_top_noselect);
                    imgTimeBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_select);
                    sort = "DESC";
                    action = MOVE_UPDATA;
                    presenter.getData(mType, 1, state, sort);

                } else {
                    timeCode = 1;
                    imgTimeTop.setBackgroundResource(R.mipmap.icon_triangle_top_select);
                    imgTimeBoom.setBackgroundResource(R.mipmap.icon_triangle_boom_noselect);
                    sort = "ASC";
                    action = MOVE_UPDATA;
                    presenter.getData(mType, 1, state, sort);
                }
                break;
        }
    }

    private void initTypeSelectData() {
        evaluateSelectBeans = new ArrayList<>();
        if (mType.equals(MYSUB)) {
            evaluateSelectBeans.add(new EvaluateSelectBean("所有预约", "0"));
            evaluateSelectBeans.add(new EvaluateSelectBean("已评价", "2"));
            evaluateSelectBeans.add(new EvaluateSelectBean("未评价", "1"));
        } else if (mType.equals(MYWAR)) {
            evaluateSelectBeans.add(new EvaluateSelectBean("所有约战", "0"));
            evaluateSelectBeans.add(new EvaluateSelectBean("已评价", "2"));
            evaluateSelectBeans.add(new EvaluateSelectBean("未评价", "1"));
        }


        evaluateSelectBeans.get(0).setSelect(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
